package com.kmwiki.Server;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.PrintWriter;

import com.kmwiki.Utility.*;
import com.kmwiki.Utility.Net.myHttp;

@SuppressWarnings("serial")
public class HtmlJson extends HttpServlet {
	
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String Url = myString.CheckNull(req.getParameter("url"),"http://go.tripexplore.net/gplus?path=photos&id=108979762451884041474&num=4");
		
	    String content = "";
	    //String Url = "http://www.google.com.tw";
	    //Url ="http://m.chinatimes.com/list.aspx?cid=1";
	    //Url = "http://www.post.gov.tw/post/index.jsp";

	    //&id=108979762451884041474&num=4&showprofile=n&bgdark=y
	    myHttp mHttp = new myHttp();
	    //content = mHttp.getURL(Url, "UTF8");
	    content = mHttp.getHttpURLConnection(Url, "UTF8");

	    myRegular mRegular = new myRegular(content);
	    String strpattern = "<title>(.*?)</title>";
	    String mTitle = mRegular.GetTextFirst(strpattern);
	    
	    com.kmwiki.myUtil.Json.ModelHtml objHtml = new com.kmwiki.myUtil.Json.ModelHtml();
	    objHtml.Url = Url;
	    objHtml.Title = mTitle;
	    objHtml.Content = content;
	    
	    Gson gson = new Gson();
	    String json = gson.toJson(objHtml);
//		resp.setContentType("text/plain; charset=utf-8");		
//		resp.getWriter().print(String.format("data(%1$s)",json));
		
	    //json = "{\"user\":\"cdsfadl\"}";
	    
	    resp.setContentType("application/json");  //; charset=utf-8
	    resp.setCharacterEncoding("UTF-8");
	    //resp.getWriter().write(json);
	    
	    PrintWriter out = resp.getWriter();
	    out.write(json);
	    out.flush();
	}
	
	
}
